package com.qin.malldiscount.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qin.malldiscount.domain.SmsHomeNewProduct;
import com.qin.malldiscount.service.SmsHomeNewProductService;
import com.qin.malldiscount.mapper.SmsHomeNewProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 覃剑
* @description 针对表【sms_home_new_product(新鲜好物表)】的数据库操作Service实现
* @createDate 2022-11-24 10:42:56
*/
@Service
public class SmsHomeNewProductServiceImpl extends ServiceImpl<SmsHomeNewProductMapper, SmsHomeNewProduct>
    implements SmsHomeNewProductService{

    @Autowired
    private SmsHomeNewProductMapper homeNewProductMapper;

    /**
     * 添加首页推荐
     */
    @Override
    public int create(List<SmsHomeNewProduct> homeNewProductList) {
        for (SmsHomeNewProduct SmsHomeNewProduct : homeNewProductList) {
            SmsHomeNewProduct.setRecommendStatus(1);
            SmsHomeNewProduct.setSort(0);
            homeNewProductMapper.insert(SmsHomeNewProduct);
        }
        return homeNewProductList.size();
    }

    /**
     * 修改推荐排序
     */
    @Override
    public int updateSort(Long id, Integer sort) {
        SmsHomeNewProduct homeNewProduct = new SmsHomeNewProduct();
        homeNewProduct.setId(id);
        homeNewProduct.setSort(sort);
        int count = homeNewProductMapper.updateById(homeNewProduct);
        return count;
    }

    /**
     * 批量删除推荐
     */
    @Override
    public int delete(List<Long> ids) {
        int count = homeNewProductMapper.deleteBatchIds(ids);
        return count;
    }

    /**
     * 更新推荐状态
     */
    @Override
    public int updateRecommendStatus(List<Long> ids, Integer recommendStatus) {
        QueryWrapper<SmsHomeNewProduct> wrapper = new QueryWrapper<>();
        wrapper.in("id",ids);
        SmsHomeNewProduct record = new SmsHomeNewProduct();
        record.setRecommendStatus(recommendStatus);
        int count = homeNewProductMapper.update(record, wrapper);
        return count;
    }

    /**
     * 分页查询推荐
     */
    @Override
    public Page<SmsHomeNewProduct> list(String productName, Integer recommendStatus, Integer pageSize, Integer pageNum) {
        Page<SmsHomeNewProduct> page = new Page<>(pageNum, pageSize);
        QueryWrapper<SmsHomeNewProduct> wrapper = new QueryWrapper<>();
        if (StrUtil.isNotEmpty(productName)){
            wrapper.like("Product_name",productName);
        }
        if (recommendStatus!=null){
            wrapper.eq("recommend_status",recommendStatus);
        }
        wrapper.orderByDesc("sort");
        Page<SmsHomeNewProduct> list = homeNewProductMapper.selectPage(page, wrapper);
        return list;
    }
}




